

//这个方法使用hash判断，如果hash有要删除的值则新数组不push，
//该方法不改变原来的数组
if(!Array.prototype.lyRemove) {
    Array.prototype.lyRemove = function (val) {
        var hash = {}
        var result = []
        var len = this.length
        var arr = this

        hash[val+typeof val] = true
        for (var i = 0; i < len; i++) {
            if (!hash[arr[i]+typeof arr[i]]) {// 处理1 和 '1'
                result.push(arr[i])
            }
        }
        return result
    }
}

var arr0 = [1,2,3,4,5,6,1,2,2,2,3]
console.log("remove 7 ",arr0.lyRemove(7)) // 删除不存在的元素，是直接返回原数组[ 1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 3 ]
console.log("remove  ",arr0.lyRemove()) // 删除不存在的元素，是直接返回原数组[ 1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 3 ]
console.log("remove a ",arr0.lyRemove('a')) // 删除不存在的元素，是直接返回原数组[ 1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 3 ]
console.log("remove 2 ",arr0.lyRemove(2)) // [ 1, 3, 4, 5, 6, 1, 3 ]
console.log("remove 1 ",arr0.lyRemove(1)) // [ 2, 3, 4, 5, 6, 2, 2, 2, 3 ]
console.log("remove 3 ",arr0.lyRemove(3)) // [ 1, 2, 4, 5, 6, 1, 2, 2, 2 ]
console.log("remove 4 ",arr0.lyRemove(4)) // [ 1, 2, 3, 5, 6, 1, 2, 2, 2, 3 ]
console.log("remove 5 ",arr0.lyRemove(5)) // [ 1, 2, 3, 4, 6, 1, 2, 2, 2, 3 ]
console.log("remove 6 ",arr0.lyRemove(6)) // [ 1, 2, 3, 4, 5, 1, 2, 2, 2, 3 ]

console.log('arr0 :',arr0) // [ 1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 3 ] 原数组不改变
console.log('-----------')





//该方法是先查找与val相等的所有下标，然后根据这些下标使用splice删除对应的值，
// splice会改变原来的数组
//该方法不改变原来的数组
if(!Array.prototype.remove) {
     Array.prototype.remove =  function(val){
         const index = []
         var len = this.length
         var arr = this
         //找出val 的所有下标，保存到数组index
         for (var i = 0; i < len; i++) {
             if (arr[i] === val) {
                 index.push(i)
             }
         }
         //根据下标Index删除所有的val
         for(var j=0;j<index.length;j++){
             arr.splice(index[j]-j,1)
         }
        return arr
    }
}

var arr = [1,2,3,4,5,6,1,2,2,2,3]

console.log(arr.remove(7)) // 删除不存在的元素，是直接返回原数组[ 1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 3 ]
console.log(arr.remove()) // 删除不存在的元素，是直接返回原数组[ 1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 3 ]
console.log(arr.remove('a')) // 删除不存在的元素，是直接返回原数组[ 1, 2, 3, 4, 5, 6, 1, 2, 2, 2, 3 ]
console.log(arr.remove(2)) // [ 1, 3, 4, 5, 6, 1, 3 ]
console.log(arr.remove(1)) // [ 3, 4, 5, 6, 3 ]
console.log(arr.remove(3)) // [ 4, 5, 6 ]
console.log(arr.remove(4)) // [ 5, 6 ]
console.log(arr.remove(5)) // [ 6 ]
console.log(arr.remove(6)) // [  ]





